<template>
  <div>
    <el-container>
      <el-container>
        <el-main>
          <el-row>
            <el-col>
              <div style="display: flex; justify-content: space-between; font-size: 16px; border-bottom: 1px solid #ccc; padding-bottom: 5px">
                <div>
                  <span class="el-icon-tickets" style="margin-right: 15px"></span>
                  <span>设备管理</span>
                </div>
                <div class="top-button">
                  <el-button size="mini" type="primary" @click="headAdd">新增</el-button>
                </div>
              </div>
            </el-col>
          </el-row>

          <div style="margin-top: 5px">
            <el-form ref="gridHeadLayout" :inline="true" :model="formInline" class="demo-form-inline">
              <el-form-item>
                <el-input
                  v-model="formInline.equipmentName"
                  size="mini"
                  placeholder="请输入设备名称">
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-select v-model="formInline.isEnable" size="mini" placeholder="请选择是否启用">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item class="top-button">
                <el-button size="mini" icon="el-icon-search" @click="handleSearch"></el-button>
                <el-button size="mini" icon="el-icon-refresh-right" @click="handleEmpty"></el-button>
              </el-form-item>
            </el-form>
          </div>

          <div>
            <div style="max-height: calc(100vh - 285px);height: calc(100vh - 285px); overflow: auto;">
              <el-row>
                <el-col :span="6" v-for="(item, index) in tableData" :key="index">
                  <el-card shadow="hover" style="margin: 5px; padding-bottom: 10px;">
                    <div style="position: relative; border-bottom: 1px solid #ccc; padding-bottom: 20px;height: 110px;">
                      <span style="position:absolute; top: 10px">
                        <!-- 根据 appType 的值显示不同的图片 -->
                        <img style="height: 70px; width: 70px" v-if="item.image != null && item.image != ''" :src="item.image" />
                        <img style="height: 70px; width: 70px" v-else src="@/assets/equipmentDefaultImg.png" />
                        <!-- 添加更多条件根据需要显示不同的图片 -->
                      </span>
                      <span style="position: absolute; top: 0; left: 80px; margin-left: 10px">
                        <strong style="font-size: 18px">{{ item.equipmentName.length > 10 ? item.equipmentName.substring(0, 10) + '...' : item.equipmentName }}</strong>
                        <span
                          :style="{
                               display: 'inline-block',
                               height: '8px',
                               width: '8px',
                               background: item.isEnable === 1 ? 'rgb(10, 191, 91)' : 'rgb(229, 69, 69)',
                               borderRadius: '7px',
                               verticalAlign: 'middle',
                               marginLeft: '10px',
                               marginBottom: '5px'}">
                        </span>
                      </span>
                      <el-tooltip class="item" effect="dark" v-if="item.isEnable == 0" content="编辑" placement="top">
                        <a style="font-size: 14px; position: absolute; right: 0" @click="rowEdit(item)">
                          <span class="el-icon-edit" style="font-size: 18px;color: #1677FF"></span>
                        </a>
                      </el-tooltip>
                      <span style="position: absolute; top: 31px; left: 80px; margin-left: 10px;font-size: 14px">
                        <span style="color: #646a73">设备编码：</span>
                        <span style="color: #1677ff">{{ item.equipmentCode }}</span>
                      </span>
                      <span style="position: absolute; top: 54px; left: 80px; margin-left: 10px;font-size: 14px">
                        <span style="color: #646a73">产品名称：</span>
                        <span style="color: #1677ff">{{ item.productName }}</span>
                      </span>
                      <span style="position: absolute; top: 77px; left: 80px; margin-left: 10px;font-size: 14px">
                        <span style="color: #646a73">关联告警：</span>
                        <span style="color: #1677ff">{{ item.warningCount }}</span>
                      </span>
                    </div>
                    <div style="margin-top: 10px;">
                      <div style="float: left">
                        <el-button size="mini" type="primary" v-if="item.isEnable == 0" style="margin: 5px"
                                   @click="rowEnable(item)">启用
                        </el-button>
                        <el-button size="mini" type="primary" v-if="item.isEnable == 1" style="margin: 5px"
                                   @click="rowDisEnable(item)">禁用
                        </el-button>
                        <el-button size="mini" v-if="item.isEnable == 0" style="margin: 5px"
                                   @click="rowRemove(item)">删除
                        </el-button>
                      </div>
                      <div style="float: right">
                        <el-button type="text" @click="rowDetail(item)" size="small">更多信息</el-button>
                      </div>
                    </div>
                  </el-card>
                </el-col>
              </el-row>
            </div>

            <el-pagination
              background
              :current-page="page.currentPage"
              :page-sizes="[8, 16, 32, 64, 128]"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              :page-size="page.pageSize"
              :pager-count="5"
              @size-change="changeSize"
              @current-change="changeCurrent"
              style="float: right; margin: 20px 0;">
            </el-pagination>
          </div>

        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import CommonTree from "@/views/components/ComTree/index.vue";
import { page as getPage, enableEquipment, disenableEquipment, remove, reStart, submit } from '@/api/cps/equipment-api'
import {formatDate} from "@/utils/dateUtils";

export default {
  name: 'equipment',
  components: {
    CommonTree,
  },
  data() {
    return {
      options: [],
      tableLoading: false,
      tableData: [],
      formInline: {},
      selectionList: [],
      searchTitle: 'cabinName',
      defaultProps: {
        label: "cabinName",
        value: "id"
      },
      page: {
        pageSize: 8,
        currentPage: 1,
        total: 0
      },
    }
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  mounted() {
    this.onLoad(this.page, {});
    this.options = this.$store.state.dict.dictDatas.is_enable
  },
  methods: {
    formatter (row, column, cellValue, index) {
      return formatDate(new Date(cellValue),"yyyy-MM-dd HH:mm:ss")
    },
    rowDetail(row) {
      this.$router.push({
        path: `/business/cps/equipment/Add`,
        query: {
          id: row.id,
          type: 'view',
        }
      })
    },
    rowEdit(row) {
      this.$router.push({
        path: `/business/cps/equipment/Add`,
        query: {
          id: row.id,
          type: 'edit',
        }
      })
    },
    headAdd() {
      this.$router.push({
        path: `/business/cps/equipment/Add`,
        query: {
          type: 'add',
        }
      })
    },
    rowRemove(row) {
      if (row.warningCount > 0) {
        this.$confirm("该设备下关联" + row.warningCount + "个告警，" +"确定删除该设备", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return remove(row);
        }).then(() => {
          this.onLoad(this.page, {});
          this.$message({
            type: "success",
            message: "操作成功"
          });
        });
      } else {
        this.$confirm("确定删除数据", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return remove(row);
        }).then(() => {
          this.onLoad(this.page, {});
          this.$message({
            type: "success",
            message: "操作成功"
          });
        });
      }

    },
    rowEnable(row) {
      // 在方法里特殊处理进行启用
      enableEquipment(row.id).then(res => {
        const {code, msg} = res;
        if (code === 0) {
          this.$message.success("操作成功")
          this.onLoad(this.page, {})
        } else {
          this.$message.success(msg);
        }
      })
    },
    rowDisEnable(row) {
      row.isEnable = 0;
      submit(row).then(res => {
        const {code, msg} = res;
        if (code === 0) {
          this.$message.success("操作成功")
        } else {
          this.$message.success(msg);
        }
      })
    },
    indexMethod (index) {
      return index + 1
    },
    handleSearch () {
      this.page.currentPage = 1
      this.onLoad(this.page, this.formInline)
    },
    handleEmpty () {
      this.formInline = {}
      this.onLoad(this.page, {})
    },
    changeSize (val) {
      this.page.pageSize = val
      this.onLoad(this.page, {})
    },
    changeCurrent (val) {
      this.page.currentPage = val
      this.onLoad(this.page, {})
    },
    onLoad(page, params) {
      this.page = page;
      this.tableLoading = true;
      getPage(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.formInline)
      ).then(res => {
        const data = res.data;
        this.page.total = data.total;
        this.tableData = data.records;
        this.tableLoading = false;
      });
    },
  }
}
</script>

<style scoped>
.equipment-card ::v-deep .el-card__body {
  padding: 0 !important;
}

.equipment-card ::v-deep .el-divider--horizontal {
  margin: 0;
}

.top-button ::v-deep .el-button + .el-button {
  margin-left: 0;
}
</style>
